package com.example.lotterysystem.common;

import com.example.lotterysystem.common.enums.GlobalErrorCodeEnum;
import lombok.Data;

import java.io.Serializable;
@Data
public class Result <T> implements Serializable {

    private Integer code;

    private String msg;

    private T data;

    public Result(){

    }

    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>();
        result.code = GlobalErrorCodeEnum.SUCCESS.getCode();
        result.msg = GlobalErrorCodeEnum.SUCCESS.getMsg();
        result.data = data;
        return result;
    }

    public static <T> Result<T> error(Integer code, String msg){
        if(!code.equals(GlobalErrorCodeEnum.SUCCESS.getCode())){
            Result<T> result = new Result<>();
            result.code = code;
            result.msg = msg;
            return result;
        }
        return success(null);
    }

    public static <T> Result<T> error(GlobalErrorCodeEnum errorCode){
        return error(errorCode.getCode(), errorCode.getMsg());
    }
}
